import { Modal, Button } from "antd";
import { History } from "history";
import React from "react";
import { Link } from "react-router-dom";
import { IResBody, MyAxios } from "../../config/http";
import { PagePath } from "../../config/page";
const { success } = Modal;

export interface IForm {
  username: string;
  password: string;
  verifyCode: string;
}

//callBack为一个回调函数
export function submitAction(
  form: IForm,
  history: History,
  setLoading: Function,
  callBack: Function
) {
  setLoading(true);
  const { username, password, verifyCode } = form;
  MyAxios.axios
    .post<any, IResBody>("user/registe", {
      username,
      password,
      verifyCode
    })
    .then(res => {
      setLoading(false);
      console.log("typeof res", typeof res);
      console.log("res", res);

      const data: string = res.data;
      console.log("请求结果", data);
      showConfirm(res.msg, history);
      callBack();
    })
    .catch(e => {
      setLoading(false);
      // 当自定义的code不等于SUCCESS时,或者当http状态为不等于200时触发
      // catch代码块中写具体业务需求的错误信息
    });
}

function showConfirm(msg: string, history: History) {
  success({
    title: msg,
    okText: "去登录!",
    onOk() {
      history.push(PagePath.LOGIN);
    },
    onCancel() {}
  });
}
